package leetcode.tree;

public class Lc543 {

    int ans = 0;

    public int diameterOfBinaryTree(TreeNode root) {
         getdiameter(root);
         return ans;
    }

    private int getdiameter(TreeNode root) {
        if (root == null) return 0;
        int l = 0;
        int r = 0;
        if (root.left != null) {
            l = getdiameter(root.left) + 1;
        }
        if (root.right != null) {
            r = getdiameter(root.right) + 1;
        }
        ans = Math.max(ans, l + r);
        return l > r ? l : r;
    }


}
